﻿using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace Monti.Docking
{
   /// <summary>
   /// Split panel for dynamic docking.
   /// </summary>
   internal class SplitPanel : Grid
   {
      /// <summary>
      /// Gets if the panel has a split or not.
      /// </summary>
      public bool HasSplit { get; private set; }

      /// <summary>
      ///  Half star grid length.
      /// </summary>
      private static readonly GridLength s_HalfStar = new GridLength (0.5, GridUnitType.Star);

      /// <summary>
      /// Sets up the given grid for a vertical drop.
      /// </summary>
      /// <param name="isToolWindow">True if we are setting up a tool window split.</param>
      /// <param name="dockArea">Position tool window will be docking to.</param>
      public void SetupVerticalGrid (bool isToolWindow, DockArea dockArea)
      {
         if (dockArea != DockArea.Top)
         {
            this.RowDefinitions.Add (new RowDefinition { MinHeight = 100, Height = new GridLength (200.0) });
            this.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
            this.RowDefinitions.Add (new RowDefinition ());
         }
         else if (dockArea == DockArea.Bottom)
         {
            this.RowDefinitions.Add (new RowDefinition ());
            this.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
            this.RowDefinitions.Add (new RowDefinition { MinHeight = 100, Height = new GridLength (200.0) });
         }
         else
         {
            this.RowDefinitions.Add (new RowDefinition { Height = s_HalfStar });
            this.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
            this.RowDefinitions.Add (new RowDefinition { Height = s_HalfStar });
         }

         var splitter = new GridSplitter
         {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Margin = new Thickness (0),
            Height = 3,
            Background = Brushes.Transparent
         };
         splitter.SetValue (RowProperty, 1);
         this.Children.Add (splitter);
         this.HasSplit = true;
      }

      /// <summary>
      /// Sets up the given grid for a horizontal drop.
      /// </summary>
      /// <param name="isToolWindow">True if we are setting up a tool window split.</param>
      /// <param name="dockArea">Position tool window will be docking to.</param>
      public void SetupHorizontalGrid (bool isToolWindow, DockArea dockArea)
      {
         if (isToolWindow && dockArea == DockArea.Left)
         {
            this.ColumnDefinitions.Add (new ColumnDefinition { MinWidth = 100, Width = new GridLength (200.0) });
            this.ColumnDefinitions.Add (new ColumnDefinition { Width = GridLength.Auto });
            this.ColumnDefinitions.Add (new ColumnDefinition ());
         }
        else if (isToolWindow && dockArea == DockArea.Right)
         {
            this.ColumnDefinitions.Add (new ColumnDefinition ());
            this.ColumnDefinitions.Add (new ColumnDefinition { Width = GridLength.Auto });
            this.ColumnDefinitions.Add (new ColumnDefinition { MinWidth = 100, Width = new GridLength (200.0) });
         }
        else
        {
            this.ColumnDefinitions.Add (new ColumnDefinition { Width = s_HalfStar });
            this.ColumnDefinitions.Add (new ColumnDefinition { Width = GridLength.Auto });
            this.ColumnDefinitions.Add (new ColumnDefinition { Width = s_HalfStar });
        }

         var splitter = new GridSplitter
         {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Margin = new Thickness (0),
            Width = 3,
            Background = Brushes.Transparent
         };
         splitter.SetValue (ColumnProperty, 1);
         this.Children.Add (splitter);
         this.HasSplit = true;
      }
   }
}
